<?php
/**
 * Created by PhpStorm.
 * User: hengsu
 * Date: 2016/5/23
 * Time: 14:31
 */

namespace Admin\Controller;
use Think\Cache\Driver\Memcache;
use Think\Controller;

class MenuController extends Controller{
    private $menu;
    public function __construct(){
        parent::__construct();
        $this->menu=M('Menu');
    }
    public function index(){
        //查询所有分类
        $list=$this->menu->select();
        //引入树形结构类
        vendor('Tree');
        //实例化树形结构类
        $tree=new \Tree();
        //初始化数据
        $tree->tree($list);
        //得到树形结构
        $menuList=$tree->getMenu();
        $this->assign('menuList',$menuList);
        $this->display();
    }
    public function menuAdd(){
        if($_POST){
            $level=($_GET['pid']==0)?1:2;
            $data=array(
                'name'=>trim($_POST['name']),
                'type'=>trim($_POST['type']),
                'key'=>trim($_POST['key']),
                'url'=>trim($_POST['url']),
                'parent_id'=>trim($_GET['pid']),
                'level'=>$level
            );
            if($this->menu->add($data)){
                $this->success('成功',U('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $this->display('add');
        }
    }
    public function editCat(){
        if($_POST){
            $data=array(
                'id'  => $_GET['id'],
                'name'=>trim($_POST['name']),
                'type'=>trim($_POST['type']),
                'key'=>trim($_POST['key']),
                'url'=>trim($_POST['url']),
            );
            if($this->menu->save($data)){
                $this->success('成功',U('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $menuli=$this->menu->where('id='.$_GET['id'])->find();
            $this->assign('menuli',$menuli);
            $this->display('edit');
        }
    }
    public function catDel(){
        $count=$this->menu->where('parent_id='.$_GET['id'])->count();
        if($count > 0){
            $this->error('该菜单下还有下一级菜单'); exit;
        }else{
            if($this->menu->where('id='.$_GET['id'])->delete()){
                $this->success('成功',U('index'));
            }else{
                $this->error('失败');
            } 
        }
        
    }
    public function setMenu(){
        $access_token=$this->accss_token();
        $url= 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
        $data=$this->menu->select();
        $arr=$this->sort($data,0);
        $ar['button']=$arr;
        $data=encode_json(json_encode($ar));
        $re=$this->https_request($url,$data);
        echo $re;
    }
    public function lll(){
        $mem = new Memcache();
        var_dump($mem->add('key','value'));
        var_dump($mem->get('key'));
    }
    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    public function accss_token(){
        $feil=file_get_contents('access/access_token.json');
        $feil=json_decode($feil);
        if($feil->expire_time<time()){
            $access=$this->acktoken();
            $feil->access_token=$access;
            $feil->expire_time=time()+6500;
            $fp = fopen('access/access_token.json', "w");
            fwrite($fp, json_encode($feil));
            fclose($fp);
            return $access;
        }else{
            return $feil->access_token;
        }
    }
    public function acktoken(){
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('APPID').'&secret='.C('APPSECRET');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $jsoninfo = json_decode($output, true);
        $access_token = $jsoninfo["access_token"];
        return $access_token;
    }
    public function sort($cate,$pid){
        $arr=array();
        foreach($cate as $v){
            if($v['parent_id'] == $pid){
                $v['sub_button']=self::sort($cate,$v['id']);
                $arr[]=$v;
            }
        }
        return $arr;
    }
}